/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package redezeitor;

import javax.swing.JOptionPane;

/**
 *
 * @author Mauriverti
 */
public class Link {
    
    public Link() {
        thisID = 0;
        otherID = 0;
        thisIP = 0;
        otherIP = 0;
        thisMask = 0;
        otherMask = 0;
        distance = 0;
    }
    
    public Link(int _idRouter1, int _idRouter2, int _IP1, int _IP2, int _mask1, int _mask2, int _distance) {
        thisID = _idRouter1;
        otherID = _idRouter2;
        thisIP = _IP1;
        otherIP = _IP2;
        thisMask = _mask1;
        otherMask = _mask2;
        distance = _distance;
    }
    
    int thisID;
    int otherID;
    int thisIP;
    int otherIP;
    int thisMask;
    int otherMask;
    int distance;

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }
    
    public int getThisID() {
        return thisID;
    }

    public void setThisID(int thisRouter) {
        this.thisID = thisRouter;
    }

    public int getOtherID() {
        return otherID;
    }

    public void setOtherID(int otherRouter) {
        this.otherID = otherRouter;
    }

    public int getThisIP() {
        return thisIP;
    }

    public void setThisIP(int thisIP) {
        this.thisIP = thisIP;
    }

    public int getOtherIP() {
        return otherIP;
    }

    public void setOtherIP(int otherIP) {
        this.otherIP = otherIP;
    }

    public int getThisMask() {
        return thisMask;
    }

    public void setThisMask(int thisMask) {
        this.thisMask = thisMask;
    }

    public int getMask2() {
        return otherMask;
    }

    public void setMask2(int mask2) {
        this.otherMask = mask2;
    }  
    
    private String getStringValue(int c) {
        String s = new String();
        s = (c & 0xFF) + "." + ((c >> 8) & 0xFF) + "." + ((c >> 16) & 0xFF) + "." + ((c >> 24) & 0xFF);
        return s;
    }

    private boolean isValid() {
        if ((thisIP & thisMask) == (otherIP & otherMask)) {
            return true;
        }
        return false;
    }
    
    public void checkSubNetwork() {
        
        if (thisIP == otherIP) {
            JOptionPane.showMessageDialog(null, thisID + " e " + otherID + " Tem o mesmo IP:\n" + getStringValue(thisIP), "", JOptionPane.INFORMATION_MESSAGE);
        }
        
        if (!isValid()) {
            JOptionPane.showMessageDialog(null, "Subrede mal configurada\n" +
                    thisID + ": " + getStringValue(thisIP & thisMask) + "\n" +
                    otherID + ": " + getStringValue(otherIP & otherMask), "", JOptionPane.INFORMATION_MESSAGE);
            
        }
        
        
    }
    
    
}
